<template>
  <div>
    <el-radio-group v-if="!multiple" v-model="selectValue" :class="direction=='horizontal'?'':'d-grid'">
      <div v-for="option in options" :key="option.value" class="img-radio-item">
        <el-radio :label="option.value" :disabled="disabled">{{ option.value }}</el-radio>
        <el-image
          :preview-src-list="[getImageSrc(option.src)]"
          :src="getImageSrc(option.src)"
          class="image"
          fit="scale-down"
        >
          <div slot="error" class="image-slot">
            <i class="el-icon-picture-outline"/>
          </div>
        </el-image>
      </div>
    </el-radio-group>
    <el-checkbox-group v-else v-model="selectValue" :class="direction=='horizontal'?'':'d-grid'">
      <div v-for="option in options" :key="option.value" class="img-radio-item">
        <el-checkbox :label="option.value" :disabled="disabled">{{ option.value }}</el-checkbox>
        <el-image
          :preview-src-list="[getImageSrc(option.src)]"
          :src="getImageSrc(option.src)"
          class="image"
          fit="scale-down"
        >
          <div slot="error" class="image-slot">
            <i class="el-icon-picture-outline"/>
          </div>
        </el-image>
      </div>
    </el-checkbox-group>
  </div>
</template>

<script>
export default {
  name: 'ImageSelect',
  props: {
    value: {
      type: String|Array,
      default: undefined
    },
    options: {
      type: Array,
      default: function () {
        return []
      },
      required: true
    },
    multiple: {
      type: Boolean,
      default: false
    },
    direction: {
      type: String,
      default: 'horizontal'
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    selectValue(val) {
      this.$emit('input', val)
    },
    value(val) {
      this.selectValue = val
    }
  },
  data() {
    return {
      selectValue: this.value
    }
  },
  methods: {
    getImageSrc(value) {
      return this.$api.getImageSrc(value)
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-radio-button__inner,
.el-radio-group,
.el-checkbox-group {
  display: flex !important;
  flex-wrap: wrap;
}

.img-radio-item {
  display: flex;
  flex-direction: column !important;
  align-content: center !important;
  align-items: center !important;
  border: 1px solid rgba(0, 0, 0, 0.1) !important;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1) !important;
  margin: 3px !important;
  background-color: #fff;

  .image {
    width: 100px;
    height: 100px;
    margin: 2px 15px 4px 15px;
  }

  .el-radio {
    margin: 4px;
  }
}

.d-grid {
  display: grid !important;
  margin-top: 5px;
  margin-bottom: 5px;
  flex-wrap: wrap;

  .img-radio-item {
    display: contents;
  }
}

::v-deep .image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  //background: #f5f7fa;
  color: #909399;
  font-size: 30px;
}
</style>
